package com.jiawa.wiki.controller;

import com.jiawa.wiki.req.CategoryQueryReq;
import com.jiawa.wiki.req.CategorySaveReq;
import com.jiawa.wiki.resp.CommonResp;
import com.jiawa.wiki.resp.CategoryQueryResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 分类管理 Controller
 *
 * @author 荀彧 2021/5/17
 */
@RestController
@RequestMapping(value = "/category")
@Slf4j
public class CategoryController {

    @Resource
    private CategoryService service;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResp<PageResp<CategoryQueryResp>> list(@Valid @RequestBody CategoryQueryReq categoryQueryReq) {
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        resp.setContent(service.list(categoryQueryReq));
        log.info("resp content is {}",resp.getContent());
        return resp;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResp save(@Valid @RequestBody CategorySaveReq saveReq) {
        CommonResp resp = new CommonResp<>();
        service.save(saveReq);
        return resp;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        service.delete(id);
        return resp;
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public CommonResp<PageResp<CategoryQueryResp>> all() {
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();

        PageResp<CategoryQueryResp> all = service.all();
        resp.setContent(all);
        return resp;
    }
}

